package com.example.lenovo_.myfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lenovo_.healthyangel.R;
import com.example.lenovo_.healthyangel_home.HealthysportFragment;
import com.example.lenovo_.healthyangel_home.JokeFragment;
import com.example.lenovo_.healthyangel_home.MaterialFragment;
import com.example.lenovo_.healthyangel_home.NewsFragment;

/**
 * Created by lenovo- on 2016/10/12.
 */
public class HomeFragment extends Fragment implements View.OnClickListener {
    private Intent intent = null;
    private ImageView mmaterialImage;
    private ImageView mhealthysportImage;
    private ImageView mnewsImage;
    private ImageView mjokeImage;
    private LinearLayout mTabmaterial;
    private LinearLayout mTabhealthysport;
    private LinearLayout mTabnews;
    private LinearLayout mTabjoke;
    private TextView mmaterialText;
    private TextView mhealthysportText;
    private TextView mnewsText;
    private TextView mjokeText;
    private MaterialFragment mMaterialFragment;
    private HealthysportFragment mHealthysportFragment;
    private NewsFragment mNewsFragment;
    private JokeFragment mJokeFragment;
    private int[] imgBtnNormal = {R.mipmap.material0, R.mipmap.healthysport0, R.mipmap.news0, R.mipmap.joke0};
    private int[] imgBtnChosen = {R.mipmap.material1, R.mipmap.healthysport1, R.mipmap.news1, R.mipmap.joke1};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_home, null);
        initView(view);
        initEvent();
        setSelect(0);
        return view;
    }

    @Override
    public void onClick(View v) {
        resetTab();
        switch (v.getId()) {
            case R.id.ll_material:
                setSelect(0);
                break;
            case R.id.ll_healthysport:
                setSelect(1);
                break;
            case R.id.ll_news:
                setSelect(2);
                break;
            case R.id.ll_joke:
                setSelect(3);
                break;
            default:
                break;
        }

    }
    private void setSelect(int i){

        FragmentTransaction transaction =  this.getFragmentManager().beginTransaction();

        hideTabFragment(transaction);

        switch (i) {
            case 0:

                if(mMaterialFragment == null){
                    mMaterialFragment = new MaterialFragment();
                    transaction.add(R.id.id_content1, mMaterialFragment);
                }else{
                    transaction.show(mMaterialFragment);
                }
                mmaterialImage.setImageResource(imgBtnChosen[0]);
                mmaterialText.setTextColor(0xff070DBA);

                break;

            case 1:

                if(mHealthysportFragment == null){
                    mHealthysportFragment = new HealthysportFragment();
                    transaction.add(R.id.id_content1, mHealthysportFragment);
                }else{
                    transaction.show(mHealthysportFragment);
                }
                mhealthysportImage.setImageResource(imgBtnChosen[1]);
                mhealthysportText.setTextColor(0xff070DBA);
                break;

            case 2:

                if(mNewsFragment == null){
                    mNewsFragment = new NewsFragment();
                    transaction.add(R.id.id_content1,mNewsFragment);
                }else{
                    transaction.show(mNewsFragment);
                }
                mnewsImage.setImageResource(imgBtnChosen[2]);
                mnewsText.setTextColor(0xff070DBA);
                break;

            case 3:

                if(mJokeFragment == null){
                    mJokeFragment = new JokeFragment();
                    transaction.add(R.id.id_content1, mJokeFragment);
                }else{
                    transaction.show(mJokeFragment);
                }
                mjokeImage.setImageResource(imgBtnChosen[3]);
                mjokeText.setTextColor(0xff070DBA);
                break;

            default:
                break;
        }
        transaction.commit();
    }

    /*
     * 隐藏fragment
     */
    private void hideTabFragment(FragmentTransaction transaction) {
        if(mMaterialFragment != null){
            transaction.hide(mMaterialFragment);
        }

        if(mHealthysportFragment != null){
            transaction.hide(mHealthysportFragment);
        }

        if(mNewsFragment != null){
            transaction.hide(mNewsFragment);
        }

        if(mJokeFragment != null){
            transaction.hide(mJokeFragment);
        }

    }

    private void resetTab() {
        mmaterialImage.setImageResource(imgBtnNormal[0]);
        mhealthysportImage.setImageResource(imgBtnNormal[1]);
        mnewsImage.setImageResource(imgBtnNormal[2]);
        mjokeImage.setImageResource(imgBtnNormal[3]);
        mmaterialText.setTextColor(0xff666666);
        mhealthysportText.setTextColor(0xff666666);
        mnewsText.setTextColor(0xff666666);
        mjokeText.setTextColor(0xff666666);
    }

    private void initView(View view) {
        mTabmaterial = (LinearLayout) view.findViewById(R.id.ll_material);
        mTabhealthysport = (LinearLayout) view.findViewById(R.id.ll_healthysport);
        mTabnews = (LinearLayout) view.findViewById(R.id.ll_news);
        mTabjoke = (LinearLayout) view.findViewById(R.id.ll_joke);
        mmaterialImage = (ImageView) view.findViewById(R.id.lv_material);
        mhealthysportImage = (ImageView) view.findViewById(R.id.lv_healthysport);
        mnewsImage = (ImageView) view.findViewById(R.id.lv_news);
        mjokeImage = (ImageView) view.findViewById(R.id.lv_joke);
        mmaterialText = (TextView) view.findViewById(R.id.tv_material);
        mhealthysportText = (TextView) view.findViewById(R.id.tv_healthysport);
        mnewsText = (TextView) view.findViewById(R.id.tv_news);
        mjokeText = (TextView) view.findViewById(R.id.tv_joke);
    }
    private void initEvent(){
        mTabmaterial.setOnClickListener(this);
        mTabhealthysport.setOnClickListener(this);
        mTabnews.setOnClickListener(this);
        mTabjoke.setOnClickListener(this);
    }

}
